package path;
import geom.Geom;
import geom.Vector;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;
import shapes.*;
import util.Color;
import util.RndGen;



public class DrawCloudPath implements Shape, PathDrawer{
	ArrayList shapes;
	float width;
	int amount;
	float avgSize;
	RndGen random;
	
	public DrawCloudPath(float width, int amount, float avgSize){
		this.width = width;
		this.amount = amount;
		this.avgSize = avgSize;
		
		shapes = new ArrayList();
		random = new RndGen();
	}
	
	public void draw(PApplet pa) {
		Iterator i = shapes.iterator();
		while(i.hasNext()){
			((Shape)i.next()).draw(pa);
		}
	}
	
	public void drawPath(Path path, PApplet pa) {
		draw(pa);
		
	}

	public void scale(float s) {
		// TODO Auto-generated method stub
		
	}
	
	public void keyAdded(Path p){
		if(p.getSize() > 2) shapes.add(new ShapeCloud(width, amount, avgSize, p.getKeyFrameByIndex(p.getSize()-2), p.getKeyFrameByIndex(p.getSize()-1), random));
	}
	
	public void keysAdded(Path p, int amount){
		
	}
	
	public void keyRemoved(Path p, int i){
		shapes.remove(i);
	}
	
	public class ShapeCloud implements Shape{
		float width;
		float length;
		KeyFrame current;
		KeyFrame next;
		ArrayList shapes;
		ArrayList positions;
		RndGen random;
		
		public ShapeCloud(float width, int amount, float avgSize, KeyFrame current, KeyFrame next, RndGen random) {
			this.width = width;
			this.length = next.getPosition().sub(current.getPosition()).absLength();
			this.current = current;
			this.next = next;
			this.random = random;
			//populate(length, width, amount, avgSize);
		}
		
		public void populate(float length, float width, int amount, float avgSize){
			shapes = new ArrayList();
			positions = new ArrayList();

			
			for(int i = 0; i < amount; i++){
				Vector v = new Vector();
				v.x = random.rndUniform((float)-length, 0);
				v.y = random.rndBetaDistributed((float)-width, (float)width, 0.5f, 4f);
				v.z = random.rndBetaDistributed((float)-width, (float)width, 0.5f, 4f);
				float size = random.rndBetaDistributed(avgSize - 0.25f * avgSize, avgSize - 0.25f * avgSize, 0.5f, 10);
				positions.add(v);
				shapes.add(new Cross(size, 1, new Color(50, 30, 235)));
			}
		}
		
		public void draw(PApplet pa){
			
		}
		
		public void drawOLD(PApplet pa) {
			pa.pushMatrix();
			Geom.orientToPath(pa, current.getPosition(), next.getPosition(), new Vector());
			
			Iterator s = shapes.iterator();
			Iterator p = positions.iterator();
			
			while(s.hasNext() && p.hasNext()){
				Vector pos = (Vector)p.next();
				pa.translate(pos.x, pos.y, pos.z);
				((Shape)s.next()).draw(pa);
			}
			
			pa.popMatrix();
		}
		
		public void scale(float s) {
			// TODO Auto-generated method stub
			
		}

	}



}
